// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import 'babel-polyfill'
import 'element-ui/lib/theme-chalk/index.css'
// fade/zoom 等
import 'element-ui/lib/theme-chalk/base.css'
// collapse 展开折叠
import CollapseTransition from 'element-ui/lib/transitions/collapse-transition'
// 阿里巴巴矢量图标库
import './assets/font/iconfont/iconfont.css'
import './styles/index.scss'
import router from './router'
import store from './store' // 状态管理
import globalVariable from '@/api/global_variable.js'
// 引入echarts
import echarts from 'echarts'
import echartsGl from 'echarts-gl'
// 引入md5
import md5 from 'js-md5'
// 富文本编辑器
import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.snow.css'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// 可拖拽弹框
import './libs/directives'
// moment 日期处理类库
import moment from 'moment'
import axios from 'axios'
import imgPreview from '@/components/ImgPreview' // 图片预览 对话框
import imgViewer from '@/components/ImgViewer' // 图片预览 大图预览
// 打印
import Print from '@/libs/print'
import VueQr from 'vue-qr'
import PopupManager from 'element-ui/lib/utils/popup/popup-manager'

import {
  Pagination,
  Dialog,
  Autocomplete,
  Dropdown,
  DropdownMenu,
  DropdownItem,
  Menu,
  Submenu,
  MenuItem,
  MenuItemGroup,
  Input,
  InputNumber,
  Radio,
  RadioGroup,
  RadioButton,
  Checkbox,
  CheckboxButton,
  CheckboxGroup,
  Switch,
  Select,
  Option,
  OptionGroup,
  Button,
  ButtonGroup,
  Table,
  TableColumn,
  DatePicker,
  TimeSelect,
  TimePicker,
  Popover,
  Tooltip,
  Breadcrumb,
  BreadcrumbItem,
  Form,
  FormItem,
  Tabs,
  TabPane,
  Tag,
  Tree,
  Alert,
  Slider,
  Icon,
  Row,
  Col,
  Upload,
  Progress,
  Badge,
  Card,
  Rate,
  Steps,
  Step,
  Carousel,
  CarouselItem,
  Collapse,
  CollapseItem,
  Cascader,
  ColorPicker,
  Transfer,
  Container,
  Header,
  Aside,
  Main,
  Footer,
  Loading,
  MessageBox,
  Message,
  Notification,
  Timeline,
  TimelineItem,
  Link,
  Divider,
  Image,
  Drawer
} from 'element-ui'

// 全局变量
Vue.prototype.GLOBAL = globalVariable

Vue.use(Pagination)
Vue.use(Dialog)
Vue.use(Autocomplete)
Vue.use(Dropdown)
Vue.use(DropdownMenu)
Vue.use(DropdownItem)
Vue.use(Menu)
Vue.use(Submenu)
Vue.use(MenuItem)
Vue.use(MenuItemGroup)
Vue.use(Input)
Vue.use(InputNumber)
Vue.use(Radio)
Vue.use(RadioGroup)
Vue.use(RadioButton)
Vue.use(Checkbox)
Vue.use(CheckboxButton)
Vue.use(CheckboxGroup)
Vue.use(Switch)
Vue.use(Select)
Vue.use(Option)
Vue.use(OptionGroup)
Vue.use(Button)
Vue.use(ButtonGroup)
Vue.use(Table)
Vue.use(TableColumn)
Vue.use(DatePicker)
Vue.use(TimeSelect)
Vue.use(TimePicker)
Vue.use(Popover)
Vue.use(Tooltip)
Vue.use(Breadcrumb)
Vue.use(BreadcrumbItem)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Tabs)
Vue.use(TabPane)
Vue.use(Tag)
Vue.use(Tree)
Vue.use(Alert)
Vue.use(Slider)
Vue.use(Icon)
Vue.use(Row)
Vue.use(Col)
Vue.use(Upload)
Vue.use(Progress)
Vue.use(Badge)
Vue.use(Card)
Vue.use(Rate)
Vue.use(Steps)
Vue.use(Step)
Vue.use(Carousel)
Vue.use(CarouselItem)
Vue.use(Collapse)
Vue.use(CollapseItem)
Vue.use(Cascader)
Vue.use(ColorPicker)
Vue.use(Transfer)
Vue.use(Container)
Vue.use(Header)
Vue.use(Aside)
Vue.use(Main)
Vue.use(Footer)
Vue.use(Timeline)
Vue.use(TimelineItem)
Vue.use(Link)
Vue.use(Divider)
Vue.use(Image)
Vue.use(Drawer)
Vue.use(VueQuillEditor)
Vue.use(moment)
Vue.use(imgPreview)
Vue.use(imgViewer)
Vue.use(Loading.directive)
Vue.use(Print) // 注册打印
Vue.use(VueQr) // 注册二维码
NProgress.configure({
  showSpinner: false
})

Vue.prototype.$loading = Loading.service
Vue.prototype.$msgbox = MessageBox
Vue.prototype.$alert = MessageBox.alert
Vue.prototype.$confirm = MessageBox.confirm
Vue.prototype.$prompt = MessageBox.prompt
Vue.prototype.$notify = Notification
Vue.prototype.$message = Message
Vue.prototype.$echarts = echarts
Vue.prototype.$echartsGl = echartsGl
Vue.prototype.$axios = axios
Vue.prototype.$md5 = md5
Vue.prototype.$moment = moment
/* eslint-disable*/
Vue.prototype.QINIUUPLOADURL = QINIUUPLOADURL

Vue.component(CollapseTransition.name, CollapseTransition)
Vue.config.productionTip = false

function isAuthedMenu(name) {
  return true
  // return store.state.userinfo.resources.filter(n => {
  //   return n.link === name
  // }).length > 0
}
const whiteList = ['/userLogin'] // 免登陆页面
router.beforeEach((to, from, next) => {
  NProgress.start()
  window.scrollTo(0, 0)
  // 解决：浏览器前进后退、消息跳转，导致dialog遮罩无法消失
  let modalStack = PopupManager.modalStack
  if (modalStack.length > 0) {
    modalStack.forEach((n) => {
      PopupManager.closeModal(n.id)
    })
  }
  if (['/login'].indexOf(to.path) < 0) {
    // 刷新或者刚从登录进入系统，获取用户信息
    if (['/', '/login'].indexOf(from.path) > -1) {
      store.dispatch('setUserInfo', function (res) {
        if (store.state.isLogin) {
          next()
        } else {
          if (whiteList.indexOf(to.path) !== -1) {
            // 在免登录白名单，直接进入
            next()
          } else {
            next({
              path: '/login'
            }) //否则全部到login
            NProgress.done()
          }
          // 
        }
      })
    } else {
      if (to.name === '404') {
        Vue.prototype.$message.error('暂无该页面权限，请联系管理员')
        next({
          name: from.name
        })
      } else if (isAuthedMenu(to.name)) {
        next()
      } else {
        Vue.prototype.$message.error('暂无该页面权限，请联系管理员')
        next({
          name: from.name
        })
      }
    }
  } else {
    next()
  }
  NProgress.done()
})

/* eslint-disable*/
window.topVue = new Vue({
  router,
  store,
  el: '#app',
  render: h => h(App)
})
